<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <link rel="stylesheet" href="__CSS__/admin.css"  media="all">
</head>
<body>
<fieldset class="layui-elem-field site-demo-button" style="margin-top: 30px;border:0"> 
    <form class="layui-form" action="{:url('admin/admin/admincate')}" method="post" lay-filter="form">
        <div class="layui-form-item" style="float: left;">
            <div class="layui-input-inline">
                <input type="text" name="keywords" lay-verify="title" autocomplete="off" placeholder="请输入关键词" class="layui-input layui-btn-sm">
            </div>
            <div class="layui-input-inline">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="create_time" placeholder="创建时间" name="create_time">
                    </div>
                </div>
            </div>
            <button class="layui-btn layui-btn-danger layui-btn-sm" lay-submit="" lay-filter="search">立即提交</button>
            <a href="{:url('admin/admin/adminCatePublish')}" class="layui-btn layui-btn-sm a_menu">添加新角色</a>
        </div>
    </form>
</fieldset>
<script type="text/html" id="authPreviewTpl">
    <a class="layui-btn layui-btn-xs" lay-event="view">点击查看</a>
</script> 
<table id="table" class="layui-table" lay-filter="table"></table>
<script type="text/html" id="operate">
	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>      
<script src="__PUBLIC__/layui/layui.js" charset="utf-8"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script>
var message;
layui.config({
    base: '__JS__/',
    version: '1.0.1'
}).use(['app', 'message'], function() {
    var app = layui.app,
        $ = layui.jquery,
        layer = layui.layer;
    //将message设置为全局以便子页面调用
    message = layui.message;
    //主入口
    app.set({
        type: 'iframe'
    }).init();
});
</script> 
<script type="text/javascript">
layui.use(['laydate','form','table','layer'],function(){
    var laydate = layui.laydate;
	var  form 	= layui.form;
	var table 	= layui.table;
    var layer   = layui.layer;

    laydate.render({
    	elem: '#create_time'
  	});

	//搜索
	form.on('submit(search)', function(data){
		tableInit.reload({
			where: {search:data.field}
		});
		return false;
  	});

	var tableInit = table.render({
		id: 'table',
		elem: '#table',
		url: "{:url('admin/admin/admincate')}",
        method: 'post',
        height: 600,
		page: true,
		cols: [[
			{checkbox:true,fixed:true,width:100},
            {field:'id',title:'编号',width:100},
            {templet:'#authPreviewTpl',title:'权限预览',width:200,align:'center'},
			{field:'name',title:'角色名称',width:100},
			{field:'create_time',title:'创建时间',width:200},
			{field:'update_time',title:'最后修改时间',width:200},
			{field:'desc',title:'备注',width:300},
			{toolbar:'#operate',title:'操作',width:505,align:'center'}
		]],
	});

	//监听工具条
	table.on('tool(table)', function(obj){
		var data = obj.data; 
		var layEvent = obj.event; 
		var tr = obj.tr;
		
		if(layEvent === 'view'){
            layer.open({
                type:2,
                content: "{:url('admin/admin/preview')}?id="+data.id,
                area: ['550px', '400px']
            });
        }else if(layEvent === 'del'){ //删除
			layer.confirm('确定要删除?', function(index){
				$.post("{:url('admin/admin/adminCateDelete')}",{id:data.id},function(res){
					if(res.code==0){
						layer.msg(res.msg,{time:1000,icon:5});
					}else{
						layer.msg(res.msg,{time:1000,icon:6});
					}
					tableInit.reload();
				});
				layer.close(index);
			});
		}else if(layEvent === 'edit'){ //编辑
			$.ajax({
				url:"{:url('admin/admin/adminCatePublish')}",
				type:'get',
				data:{id:data.id},
				success: function(res){
					if(res.code === 0){
						layer.msg('你没有权限!');
					}else{
						layer.open({
							type:2,
							content:["{:url('admin/admin/adminCatePublish')}?id="+data.id,'no'],
							area: ['700px', '600px'],
							maxmin: true,
							end:function(){
								tableInit.reload();
							}
						});
					}
				},
				error: function(){
					layer.msg('请求失败!');
				}
			});
		}
	});
});
</script>
<!-- <script type="text/javascript">
$('.delete').click(function(){
  var id = $(this).attr('id');
  layer.confirm('确定要删除?', function(index) {
    $.ajax({
      url:"{:url('admin/admin/adminCateDelete')}",
      data:{id:id},
      success:function(res) {
        layer.msg(res.msg);
        if(res.code == 1) {
          setTimeout(function(){
            location.href = res.url;
          },1500)
        }
      }
    })
  })
})
</script> -->
<script type="text/javascript">
layui.use('layer', function(){
  var layer = layui.layer;
  $('.a_menu').click(function(){
    var url = $(this).attr('href');
    var a = 0;
    $.ajax({
      url:url
      ,async:false
      ,success:function(res){
        if(res.code == 0) {
          layer.msg(res.msg);
          a = 1;
        }
      }
    })
    if(a === 0) {
      layer.open({
        type:2,
        content:url,
        area: ['700px', '600px'],
        end:function(){
          location.reload();
        }
      });
    }
    return false;
  })

  $('.preview').click(function(){
    var url = $(this).attr('href');
    layer.open({
      type:2,
      content:url,
      area: ['550px', '400px']
    });
    return false;
  })
});
</script>
</body>
</html>
